package dao

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"github.com/e421083458/gin_scaffold/public"
	"github.com/e421083458/gorm"
	"github.com/gin-gonic/gin"
	"io"
	"log"
	"time"
)

type Aritcle2 struct {
	Id          int    `json:"id"`
	Logo        string `json:"logo"`
	Title       string `json:"title"`
	Addtime     string `json:"addtime"`
	Reads       int    `json:"reads"`
	TitleStatus int    `json:"title_status"`
}
type Aritcle struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Content     string `json:"content"`
	Addtime     string `json:"addtime"`
	Logo        string `json:"logo"`
	Reads       int    `json:"reads"`
	TitleStatus int    `json:"title_status"`
}
type Area struct {
	Id        int       `json:"id" gorm:"primary_key" description:"自增主键"`
	AreaName  string    `json:"area_name" gorm:"column:area_name" description:"区域名称"`
	CityId    int       `json:"city_id" gorm:"column:city_id" description:"城市id"`
	UserId    int64     `json:"user_id" gorm:"column:user_id" description:"操作人"`
	UpdatedAt time.Time `json:"update_at" gorm:"column:update_at" description:"更新时间"`
	CreatedAt time.Time `json:"create_at" gorm:"column:create_at" description:"创建时间"`
}
type Users struct {
	Id        int     `json:"id"`
	Username  string  `json:"username"`
	Token     string  `json:"token"`
	Status    int     `json:"status"`
	PayPoints float64 `json:"pay_points"`
}

func (t *Area) TableName() string {
	return "area"
}

//验证用户登录
func (t *EveryList) FindNewsList(c *gin.Context, tx *gorm.DB) (*EndReturn, error) {
	var list1 []EveryList
	err := tx.Table("xa_article").Where("news_category_id = 1 and role=1").Find(&list1).Error
	log.Println(list1)
	if err != nil {
		return nil, err
	}
	var list2 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=2 and role=1").Find(&list2).Error
	if err != nil {
		return nil, err
	}
	var list3 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=3 and role=1").Find(&list3).Error
	if err != nil {
		return nil, err
	}
	var list4 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=4 and role=1").Find(&list4).Error
	if err != nil {
		return nil, err
	}
	return &EndReturn{
		Ev1: list1,
		Ev2: list2,
		Ev3: list3,
		Ev4: list4,
	}, nil
}
func (t *EveryList) FindNewsList2(c *gin.Context, tx *gorm.DB) (*MoveEndReturn, error) {
	var list1 []EveryList
	err := tx.Table("xa_article").Where("news_category_id = 1 and role=2").Find(&list1).Error
	log.Println(list1)
	if err != nil {
		return nil, err
	}
	var list2 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=2 and role=2").Find(&list2).Error
	if err != nil {
		return nil, err
	}
	var list3 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=3 and role=2").Find(&list3).Error
	if err != nil {
		return nil, err
	}
	var list4 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=4 and role=2").Find(&list4).Error
	if err != nil {
		return nil, err
	}
	var list5 []EveryList
	err = tx.Table("xa_article").Where("news_category_id=5 and role=2").Find(&list5).Error
	if err != nil {
		return nil, err
	}
	return &MoveEndReturn{
		Ev1: list1,
		Ev2: list2,
		Ev3: list3,
		Ev4: list4,
		Ev5: list5,
	}, nil
}

type MoveEndReturn struct {
	Ev1 []EveryList `json:"ev_1"`
	Ev2 []EveryList `json:"ev_2"`
	Ev3 []EveryList `json:"ev_3"`
	Ev4 []EveryList `json:"ev_4"`
	Ev5 []EveryList `json:"ev_5"`
}
type EndReturn struct {
	Ev1 []EveryList `json:"ev_1"`
	Ev2 []EveryList `json:"ev_2"`
	Ev3 []EveryList `json:"ev_3"`
	Ev4 []EveryList `json:"ev_4"`
}

type EveryList struct {
	Id             int    `json:"id"`
	Title          string `json:"title"`
	NewsCategoryId string `json:"news_category_id"`
	Content        string `json:"omitempty"`
	Browse         string `json:"browse"`
	Cover          string `json:"img"`
	Creatime       string `json:"pushtime"`
}

//验证用户登录
func (t *Users) FindUserToken(c *gin.Context, tx *gorm.DB, id string, token string) (*Users, error) {
	user := &Users{}
	err := tx.Table("user").Where("id = ? and token =?", id, token).Find(user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}

type NewsGetFour struct {
	Id       int    `json:"id"`
	Cover    string `json:"logo"`
	Title    string `json:"title"`
	Creatime string `json:"addtime"`
	Browse   int    `json:"reads"`
}

func (t *NewsGetFour) Article(c *gin.Context, tx *gorm.DB) ([]EveryList, error) {
	var user []EveryList
	err := tx.Table("xa_article").Where("role=1").Limit(4).Find(&user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (t *EveryList) Articleone(c *gin.Context, tx *gorm.DB, id int) (*EveryList, error) {
	//var user *Aritcle
	user := &EveryList{}
	err := tx.Table("xa_article").Where("id=?", id).Find(&user).Error
	if err != nil {
		return nil, err
	}
	fmt.Println(user)
	return user, nil
}

//用户登录
func (t *Users) FindUser(c *gin.Context, tx *gorm.DB, id string) (*Users, error) {
	user := &Users{}
	err := tx.Table("user").Where("id = ?", id).Find(user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}
func (t *Users) Login(c *gin.Context, tx *gorm.DB, name string, pwd string) (error, *Users, string) {
	user := &Users{}
	//var user User
	//password := md5.Sum([]byte (pwd))
	password := md5.New()
	io.WriteString(password, pwd)

	err := tx.Table("user").Where("username = ? and password=?", name, hex.EncodeToString(password.Sum(nil))).Find(user).Error
	//fmt.Println(hex.EncodeToString(password.Sum(nil)))
	if err != nil {
		return err, nil, ""
	}
	//生成token 持久存储 且返回
	res := public.Generate(time.Now())
	err = tx.Table("user").Where("id=?", user.Id).Update("token", res).Error
	if err != nil {
		return err, nil, ""
	}
	return nil, user, res
}
func (t *Area) Find(c *gin.Context, tx *gorm.DB, id string) (*Area, error) {
	area := &Area{}
	err := tx.SetCtx(public.GetGinTraceContext(c)).Where("id = ?", id).Find(area).Error
	if err != nil {
		return nil, err
	}
	return area, nil
}

func (t *Area) Update(c *gin.Context, tx *gorm.DB, id string, areaa string) (int, error) {
	area := &Area{}
	area.AreaName = areaa
	err := tx.Table("area").Where("id = ?", id).Updates(area).Error

	if err != nil {
		return 0, err
	}
	return 1, nil
}
